
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;



public class ControlServerCommunication extends Thread {
	private String hostName;
	private int port;
	private Integer serverPort;
	private DataServerInfo dsInfo;
	private ServerSocketChannel server;
	private ObjectOutputStream objOutputStream;
	private ObjectInputStream objInputStream;
	private final String IS_READY = "READY";
	private SocketChannel socket;
	private ByteBuffer clientBuffer = ByteBuffer.allocate(1024);
	
	public ControlServerCommunication(String hostName, int port, int serverPort){
		this.hostName = hostName;
		this.port = port;
		this.serverPort = serverPort;		
	}
	
	
	public void run(){
		System.out.println("In control server comm..");
		try{
			socket = SocketChannel.open(new InetSocketAddress(this.hostName, this.port));
			InetAddress hostMachine = InetAddress.getLocalHost();
			System.out.println("Socket initialized");
			String serverHost = hostMachine.getHostAddress();
			
			OutputStream outStream = socket.socket().getOutputStream();
			InputStream inStream = socket.socket().getInputStream();
			//System.out.println(serverPort);
			objOutputStream = new ObjectOutputStream(outStream);
			objOutputStream.writeUnshared("Data Server");
		
			objOutputStream.writeUnshared(serverHost);
			objOutputStream.writeUnshared(serverPort);
			System.out.println("DONE");
			objInputStream = new ObjectInputStream(inStream);
			
			
			while(true){
				Object in = objInputStream.readUnshared();

				if(in instanceof String){
					String chunkName = (String)in;
					System.out.println(chunkName);
					long sizeOfChunk = (Long)objInputStream.readUnshared();
					FileOutputStream chunkIn = new FileOutputStream(chunkName);
					FileChannel chunkChannelAtDataServer = chunkIn.getChannel();
					chunkChannelAtDataServer.transferFrom(socket, 0, sizeOfChunk);
					chunkChannelAtDataServer.close();
					chunkIn.close();
				//	File f  = (File)objInputStream.readUnshared();
				//	FileOutputStream fout = new FileOutputStream(f);
				//	FileInputStream fin = new FileInputStream(f);
				//	FileChannel fChan = fout.getChannel();
				//	fChan.transferFrom(socket, 0,3000000 );
				}else{
					System.out.println("INCORRECT INSTANCE RECEIVED****************");
				}
			}
		}catch(Exception e){
			try{
				System.out.println("Socket error");
				//e.printStackTrace();
				socket.close();
			}catch(Exception eX){
				//eX.printStackTrace();
			}
			
		}
	}
	
	
}
